<?php

/**
* 
*/
class DBManager
{

	protected $conn;

	public function __construct($dbFile="data.db"){
    # code...
		try {
			$config = parse_ini_file(realpath(dirname(__FILE__) . '/config/config.ini'));
			$servername = $config['host'];
			$username = $config['username'];
			$password = $config['password'];
			$dbname = $config['dbname'];
			$this->conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
		}
		catch(PDOException $e)
		{
			echo $e->getMessage();
		}
	}



	public function save($type,$data,$time,$clientip,$source,$code,$version)
	{
		//存入数据来源
		$stmt = $this->conn->prepare("call db_monitor.insertSourseIfNeedPro(?)");
		$stmt->bindParam(1,$source); 
		// 调用存储过程
		$stmt->execute();

		//$data = str_replace("\n", "", $data);
        # code...
		$sql = "INSERT INTO log(level,data,time,clientip,source,code,version)  VALUES ('{$type}','{$data}','{$time}','{$clientip}','{$source}','{$code}','{$version}');";//,strftime('%s','now')
		//echo "$sql";
		return $this->safeExecSql($sql);
	}

	public function safeExecSql($sql){
		try {
			$this->conn->exec($sql); 
		}
		catch(PDOException $e)
		{
			return $e->getMessage();
		}
		return "";
	}


	function __destruct(){
		//关闭连接
		$this->conn = null;
	}

}


?>